package com.penghaisoft.microwms.producthand.web.controller.insidewarehouse;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.data.model.ProBarcode;
import com.penghaisoft.microwms.common.data.model.ProHandOutSn;
import com.penghaisoft.microwms.common.data.model.ProSellOutSn;
import com.penghaisoft.microwms.common.dto.HandResp;
import com.penghaisoft.microwms.common.dto.ProCountingDto;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;
import com.penghaisoft.microwms.common.wms.model.ProCountingDifference;
import com.penghaisoft.microwms.common.wms.model.ProCountingRecords;
import com.penghaisoft.microwms.common.wms.model.ProCountingTask;
import com.penghaisoft.microwms.common.wms.model.ProOrderInSn;
import com.penghaisoft.microwms.common.wms.model.ProScrapOutSn;
import com.penghaisoft.microwms.common.wms.model.ProStorageSn;
import com.penghaisoft.microwms.product.service.IProBarcodeService;
import com.penghaisoft.microwms.product.service.IProCountingDifferenceService;
import com.penghaisoft.microwms.product.service.IProCountingRecordsService;
import com.penghaisoft.microwms.product.service.IProCountingTaskService;
import com.penghaisoft.microwms.product.service.IProHandInSnService;
import com.penghaisoft.microwms.product.service.IProHandOutSnService;
import com.penghaisoft.microwms.product.service.IProOrderInSnService;
import com.penghaisoft.microwms.product.service.IProScrapOutSnService;
import com.penghaisoft.microwms.product.service.IProSellOutSnService;
import com.penghaisoft.microwms.product.service.IProStorageSnService;

/**
 * @ClassName: ProCountingHandController
 * @Description: 盘点手持
 * @author jzh
 * @date 2018年6月13日 上午9:41:25
 */
@RestController
@RequestMapping(value = "/pro/countinghand")
public class ProCountingHandController extends BaseController {

	@Autowired
	private IProCountingTaskService proCountingTaskService;
	
	@Autowired
	private IProStorageSnService proStorageSnService;

	@Autowired
	private IProCountingRecordsService proCountingRecordsService;
	
	@Autowired
	private IProCountingDifferenceService proCountingDifferenceService;
	
	@Autowired
	private IProHandOutSnService proHandOutSnService;
	
	@Autowired
	private IProSellOutSnService proSellOutSnService;
	
	@Autowired
	private IProScrapOutSnService proScrapOutSnService;
	
	@Autowired
	private IProOrderInSnService proOrderInSnService;
	
	@Autowired
	private IProBarcodeService proBarcodeService;
	
	/**
	 * 查询列表
	 * 
	 * @param pager
	 * @return
	 */
	@PostMapping("list")
	public TransResult list(@RequestParam(name = "page", defaultValue = "1") int page,
			@RequestParam(name = "rows", defaultValue = "10") int rows, ProCountingTask proCountingTask) {
		Pager<ProCountingTask> result = proCountingTaskService.findListByCondition(page, rows, proCountingTask);
		return success(result);
	}
	
	
    /** 
    * @Title: countingCodeExist 
    * @Description: 判断盘点单号是否存在
    * @param @param rawBackDto
    * @param @return  参数说明 
    * @return HandResp    返回类型
    * @author jzh 
    * @throws 
    */
    @PostMapping("countingcode")
    public HandResp countingCodeExist(@RequestBody ProCountingDto proCountingDto){
        HandResp result = new HandResp();
        //Code先置为1SUCCESS
        result.setCode(Constant.SUCCESS);
        if (proCountingDto.getCountingCode().equals("")||proCountingDto.getCountingCode()==null){
            result.setCode(Constant.FAIL);
            result.setMsg("请输入盘点单号！");
            return result;
        }
        //设置查询标志（CountingCode=？ ，ActiveFlag = 1）
        ProCountingTask proCountingTask = new ProCountingTask();
        proCountingTask.setCountingCode(proCountingDto.getCountingCode());
        proCountingTask.setActiveFlag("1");
       
        List<ProCountingTask> tmp = proCountingTaskService.queryByAny(proCountingTask);
        if(tmp.size() > 0){
        	if(tmp.get(0).getStauts().equals("0")){
        		result.setMsg("盘点任务存在，请盘点");
        		//记录last_modified_by，gmt_modified	最后修改人，最后修改时间
        		proCountingTask.setLastModifiedBy(proCountingDto.getLoginName());
        		proCountingTask.setGmtModified(new Date());
        		//根据countingTaskId修改
        		proCountingTask.setCountingTaskId(tmp.get(0).getCountingTaskId());
        		Resp resp = new Resp();
        		//将盘点任务状态置为盘点中
        		resp = proCountingTaskService.updateProCountingTask(proCountingTask);
        		result.setCode(resp.getCode());
        		if (resp.getMsg() != null && !resp.getMsg().equals("")) {
        			result.setMsg(resp.getMsg());
        		}
        	}else if(tmp.get(0).getStauts().equals("1")){
//        		result.setCode(Constant.FAIL);
            	result.setMsg("继续盘点");
        	}else if(tmp.get(0).getStauts().equals("2")){
        		result.setCode(Constant.FAIL);
            	result.setMsg("盘点任务已完成");
        	}else{
        		result.setCode(Constant.FAIL);
        		result.setMsg("盘点任务未知状态");
        	}
        }else{
        	result.setCode(Constant.FAIL);
        	result.setMsg("盘点任务不存在！");
        }
        return result;
    }
    
    /** 
    * @Title: barcodeExist 
    * @Description: 扫码盘点
    * @author jzh 
    */
//    @PostMapping("barcodeisexist")
//    public HandResp barcodeExist(@RequestBody ProCountingDto proCountingDto){
//        HandResp result = new HandResp();
//        //入参条码，盘点单号，LoginName
//        String barcode= proCountingDto.getBarcode();
//        String countingCode = proCountingDto.getCountingCode();
//        String LoginName = proCountingDto.getLoginName();
//        //根据盘点单号查询合同号,型号类型
//        ProCountingTask countingByCode = new ProCountingTask();
//        countingByCode.setCountingCode(countingCode);
//        List<ProCountingTask> tmp = proCountingTaskService.queryByAny(countingByCode);
//        String contractNo = tmp.get(0).getContractNo();
//        //到rawStorageSn中查询(两种盘点类型)
//        ProStorageSn proStorageSn = new ProStorageSn();
//        if(tmp.get(0).getModelType()!=null||!tmp.get(0).getModelType().equals("")){
//        	//类型盘点
//        	String modelType = tmp.get(0).getModelType();
//        	//根据条码，合同号,类型 到rawStorageSn中查询，判断当前条码是否在合同号下
//        	proStorageSn.setModelType(modelType);
//            proStorageSn.setBarcode(barcode);
//            proStorageSn.setContractNo(contractNo);
//            proStorageSn.setActiveFlag("1");
//        }else{
//        	//合同号盘点
//	        //根据条码，合同号到rawStorageSn中查询，判断当前条码是否在合同号
//	        proStorageSn.setBarcode(barcode);
//	        proStorageSn.setContractNo(contractNo);
//	        proStorageSn.setActiveFlag("1");
//        }
//        List<ProStorageSn> list = proStorageSnService.queryByAny(proStorageSn);
//        if(list.size()>0){
//        	//当前条码存在合同号下
//        	ProCountingRecords proCountingRecords = new ProCountingRecords();
//        	proCountingRecords.setCountingCode(countingCode);
//        	proCountingRecords.setBarcode(barcode);
//        	//查看这个条码是否已经被盘点了countingCode barcode
//        	List<ProCountingRecords> proCountingRecordsList =proCountingRecordsService.queryByAny(proCountingRecords);
//        	if(proCountingRecordsList.size()>0){
//        		result.setCode(Constant.FAIL);
//            	result.setMsg("改条码已经被盘点了，请继续扫描");
//        	}else{
//        		//创建盘点记录
//        		proCountingRecords.setCountingTime(new Date());
//        		proCountingRecords.setModel(list.get(0).getModel());
//        		proCountingRecords.setModelDesc(list.get(0).getModelDesc());
//        		proCountingRecords.setModelType(list.get(0).getModelType());
//        		proCountingRecords.setWhCode(list.get(0).getWlCode());
//        		proCountingRecords.setWdCode(list.get(0).getWdCode());
//        		proCountingRecords.setWaCode(list.get(0).getWaCode());
//        		proCountingRecords.setWlCode(list.get(0).getWlCode());
//        		proCountingRecords.setKeepBy(LoginName);
//        		proCountingRecords.setCreateBy(LoginName);
//        		proCountingRecords.setGmtCreate(new Date());
//        		proCountingRecords.setActiveFlag("1");
//        		if(list.get(0).getModelType()!=null||!list.get(0).getModelType().equals("")){
//        			proCountingRecords.setModelType(list.get(0).getModelType());
//        		}
//		    	Resp resp = proCountingRecordsService.create(proCountingRecords);
//		    	//返回手持页面信息（当前扫描条码信息）
//		        List Countinglist = new ArrayList<>();
//		        if(list.get(0).getModelType()!=null||!list.get(0).getModelType().equals("")){
//		        	proCountingDto.setModelType(list.get(0).getModelType());
//        		}
//		        proCountingDto.setModel(list.get(0).getModel());
//		        proCountingDto.setModelDesc(list.get(0).getModelDesc());
//		        proCountingDto.setModelType(list.get(0).getModelType());
//		        proCountingDto.setWhCode(list.get(0).getWlCode());
//		        proCountingDto.setWdCode(list.get(0).getWdCode());
//		        proCountingDto.setWaCode(list.get(0).getWaCode());
//		        proCountingDto.setWlCode(list.get(0).getWlCode());
//		        proCountingDto.setAmount(list.get(0).getAmount());
//		        proCountingDto.setKeepBy(LoginName);
//		        //当前盘点数量
//		        int nowCountingNum = 0;
//		        //根据盘点单号在盘点记录表中查询已盘点数量
//		        nowCountingNum = proCountingRecordsService.querCountByCountingCode(countingCode);
//		        proCountingDto.setNowCountingNum(nowCountingNum);
//		        Countinglist.add(proCountingDto);
//	            result.setList(Countinglist);
//	            result.setCode(resp.getCode());
//	            result.setMsg(resp.getMsg());
//        	}
//        
//        }else{
//        	result.setCode(Constant.FAIL);
//        	result.setMsg("当前条码不是该合同或类型的条码，请继续扫描");
//        }
//        return result;
//    }
    /** 
    * @Title: barcodeExist 
    * @Description: 扫码盘点
    * @author yanggm 
    * @date 2018年8月21日 下午3:12:39 
    * @throws 
    */
    @PostMapping("barcodeisexist")
    public HandResp barcodeExist(@RequestBody ProCountingDto proCountingDto){
        HandResp result = new HandResp();
        String LoginName = proCountingDto.getLoginName();
        
        ProCountingRecords proCountingRecords = new ProCountingRecords();
    	proCountingRecords.setCountingCode(proCountingDto.getCountingCode());
    	proCountingRecords.setBarcode(proCountingDto.getBarcode());
    	//查看这个条码是否已经被盘点了countingCode barcode
    	List<ProCountingRecords> proCountingRecordsList =proCountingRecordsService.queryByAny(proCountingRecords);
    	if(proCountingRecordsList.size()>0){
    		result.setCode(Constant.FAIL);
        	result.setMsg("该条码已经被盘点了，请继续扫描");
        	return result;
    	}
    	Resp resp = new Resp ();
        
        ProStorageSn proStorageSn = new ProStorageSn();
        proStorageSn.setBarcode(proCountingDto.getBarcode());
        proStorageSn.setActiveFlag("1");
        List<ProStorageSn> list = proStorageSnService.queryByAny(proStorageSn);
        if(list.size()>0){
    		//创建盘点记录
    		proCountingRecords.setCountingTime(new Date());
    		proCountingRecords.setModel(list.get(0).getModel());
    		proCountingRecords.setWlCode(list.get(0).getWlCode());
    		proCountingRecords.setKeepBy(LoginName);
    		proCountingRecords.setCreateBy(LoginName);
    		proCountingRecords.setGmtCreate(new Date());
    		proCountingRecords.setActiveFlag("1");
	    	resp = proCountingRecordsService.create(proCountingRecords);
	    	if("2".equals(list.get(0).getBarcodeStatus())){
	    		ProCountingDifference proCountingDifference = new ProCountingDifference();
        		proCountingDifference.setCountingCode(proCountingDto.getCountingCode());
        		proCountingDifference.setCountingTime(new Date());//盘点时间记录当前时间了
        		proCountingDifference.setProBarcode(proCountingDto.getBarcode());
        		proCountingDifference.setModel(list.get(0).getModel());
        		proCountingDifference.setDifferType("0");//盘盈
        		proCountingDifference.setUserDefined1(Reason(proCountingDto.getBarcode()));
        		proCountingDifference.setDifferStatus("0");//未处理
        		proCountingDifference.setWlCode(list.get(0).getWlCode());
        		proCountingDifference.setKeepBy(LoginName);//保管员记录为当前登录人了
        		proCountingDifference.setGmtCreate(new Date());
        		proCountingDifference.setActiveFlag("1");
        		proCountingDifference.setCreateBy(LoginName);
        		proCountingDifferenceService.create(proCountingDifference);
	    	}
	    	//返回手持页面信息（当前扫描条码信息）
	        proCountingDto.setModel(list.get(0).getModel());
	        proCountingDto.setWlCode(list.get(0).getWlCode());
	        proCountingDto.setWeight(list.get(0).getWeight());
//	        proCountingDto.setKeepBy(LoginName);
        }
        else{
        	ProBarcode proBarcode = new ProBarcode();
        	proBarcode.setBarcode(proCountingDto.getBarcode());
        	List<ProBarcode> barcodes = proBarcodeService.queryList(proBarcode);
        	if(barcodes.size()>0){
	        	proCountingRecords.setCountingTime(new Date());
	        	proCountingRecords.setModel(barcodes.get(0).getModel());
	        	proCountingRecords.setKeepBy(LoginName);
	    		proCountingRecords.setCreateBy(LoginName);
	    		proCountingRecords.setGmtCreate(new Date());
	    		proCountingRecords.setActiveFlag("1");
		    	resp = proCountingRecordsService.create(proCountingRecords);
		    	
		    	ProCountingDifference proCountingDifference = new ProCountingDifference();
        		proCountingDifference.setCountingCode(proCountingDto.getCountingCode());
        		proCountingDifference.setCountingTime(new Date());//盘点时间记录当前时间了
        		proCountingDifference.setProBarcode(proCountingDto.getBarcode());
        		proCountingDifference.setModel(barcodes.get(0).getModel());
        		proCountingDifference.setDifferType("0");//盘盈
        		proCountingDifference.setUserDefined1(Reason(proCountingDto.getBarcode()));
        		proCountingDifference.setDifferStatus("0");//未处理
//        		proCountingDifference.setWlCode(list.get(0).getWlCode());
        		proCountingDifference.setKeepBy(LoginName);//保管员记录为当前登录人了
        		proCountingDifference.setGmtCreate(new Date());
        		proCountingDifference.setActiveFlag("1");
        		proCountingDifference.setCreateBy(LoginName);
        		proCountingDifferenceService.create(proCountingDifference);
		    	
        		proCountingDto.setModel(barcodes.get(0).getModel());
//		        proCountingDto.setKeepBy(LoginName);
        	}
        	else{
        		result.setCode(Constant.FAIL);
            	result.setMsg("当前条码未下线，请继续扫描");
            	return result;
        	}
        }
        List<ProCountingDto> Countinglist = new ArrayList<>();
        //根据盘点单号在盘点记录表中查询已盘点数量
        ProCountingRecords countingRecords = new ProCountingRecords();
        countingRecords.setCountingCode(proCountingDto.getCountingCode());
        Long nowCountingNum = proCountingRecordsService.queryCount(countingRecords);
        
        proCountingDto.setNowCountingNum(nowCountingNum);
        Countinglist.add(proCountingDto);
        result.setList(Countinglist);
        result.setCode(resp.getCode());
        result.setMsg(resp.getMsg());
        return result;
    }
    
    /** 
    * @Title: countingConfig 
    * @Description: 成品盘点确认
    * @param @param rawCountingDto
    * @param @return  参数说明 
    * @return HandResp    返回类型
    * @author jzh 
    * @throws 
    */
//    @PostMapping("countingconfig")
//    public HandResp countingConfig(@RequestBody ProCountingDto proCountingDto){
//        HandResp result = new HandResp();
//        //入参：盘点单号，LoginName
//        String countingCode = proCountingDto.getCountingCode();
//        String LoginName = proCountingDto.getLoginName();
//        //根据盘点单号查询合同号
//        ProCountingTask countingByCode = new ProCountingTask();
//        countingByCode.setCountingCode(countingCode);
//        List<ProCountingTask> tmp = proCountingTaskService.queryByAny(countingByCode);
//        if(tmp.size()>0){
//	        String contractNo = tmp.get(0).getContractNo();
//	        proCountingDto.setContractNo(contractNo);
//	        if(tmp.get(0).getModelType()!=null||!tmp.get(0).getModelType().equals("")){
//	        	proCountingDto.setModelType(tmp.get(0).getModelType());
//	        }
//	        //根据盘点单号，合同号 (型号类型)查询盘亏数据
//	        List<ProStorageSn> listLose = proStorageSnService.queryLose(proCountingDto);
//	        if(listLose.size()>0){
//	        	//盘亏，存在盘亏数据
//	        	for(ProStorageSn proStorageSn:listLose){
//	        		//创建盘亏差异记录
//	        		ProCountingDifference proCountingDifference = new ProCountingDifference();
//	        		proCountingDifference.setCountingCode(countingCode);
//	        		proCountingDifference.setContractNo(contractNo);
//	        		proCountingDifference.setPurchaseNo(proStorageSn.getPurchaseNo());
//	        		proCountingDifference.setCountingTime(new Date());//盘点时间记录当前时间了
//	        		proCountingDifference.setProBarcode(proStorageSn.getBarcode());
//	        		proCountingDifference.setModel(proStorageSn.getModel());
//	        		proCountingDifference.setModelDesc(proStorageSn.getModelDesc());
//	        		proCountingDifference.setModelType(proStorageSn.getModelType());
//	        		proCountingDifference.setDifferType("1");//盘亏
//	        		proCountingDifference.setDifferStatus("0");//未处理
//	        		proCountingDifference.setWaCode(proStorageSn.getWaCode());
//	        		proCountingDifference.setWdCode(proStorageSn.getWdCode());
//	        		proCountingDifference.setWhCode(proStorageSn.getWhCode());
//	        		proCountingDifference.setWlCode(proStorageSn.getWlCode());
//	        		proCountingDifference.setKeepBy(LoginName);//保管员记录为当前登录人了
//	        		proCountingDifference.setGmtCreate(new Date());
//	        		proCountingDifference.setActiveFlag("1");
//	        		proCountingDifference.setCreateBy(LoginName);
//	        		Resp resp = proCountingDifferenceService.create(proCountingDifference);	
//	        	}
//	        }
//	        //根据盘点单号，合同号 查询盘盈数据
//	        List<ProCountingRecords> listWin = proCountingRecordsService.queryWin(proCountingDto);
//	        if(listWin.size()>0){
//	        	for(ProCountingRecords proCountingRecords : listWin){
//	        		//创建盘盈差异记录
//	        		ProCountingDifference proCountingDifference = new ProCountingDifference();
//	        		proCountingDifference.setCountingCode(countingCode);
//	        		proCountingDifference.setContractNo(contractNo);
//	        		//proCountingDifference.setPurchaseNo(rawCountingRecords.getPurchaseNo());
//	        		proCountingDifference.setCountingTime(new Date());//盘点时间记录当前时间了
//	        		proCountingDifference.setProBarcode(proCountingRecords.getBarcode());
//	        		proCountingDifference.setModel(proCountingRecords.getModel());
//	        		proCountingDifference.setModelDesc(proCountingRecords.getModelDesc());
//	        		proCountingDifference.setModelType(proCountingRecords.getModelType());
//	        		proCountingDifference.setDifferType("0");//盘盈
//	        		proCountingDifference.setDifferStatus("0");//未处理
//	        		proCountingDifference.setWaCode(proCountingRecords.getWaCode());
//	        		proCountingDifference.setWdCode(proCountingRecords.getWdCode());
//	        		proCountingDifference.setWhCode(proCountingRecords.getWhCode());
//	        		proCountingDifference.setWlCode(proCountingRecords.getWlCode());
//	        		proCountingDifference.setKeepBy(LoginName);//保管员记录为当前登录人了
//	        		proCountingDifference.setGmtCreate(new Date());
//	        		proCountingDifference.setActiveFlag("1");
//	        		proCountingDifference.setCreateBy(LoginName);
//	        		Resp resp = proCountingDifferenceService.create(proCountingDifference);	
//	        	}
//	        }
//	        ProCountingTask proCountingTask = new ProCountingTask();
//	        proCountingTask.setLastModifiedBy(proCountingDto.getLoginName());
//	        proCountingTask.setGmtModified(new Date());
//			//根据countingTaskId修改
//	        proCountingTask.setCountingTaskId(tmp.get(0).getCountingTaskId());
//			Resp resp = new Resp();
//			//将盘点任务状态置为盘点结束
//			proCountingTask.setStauts("2");
//			resp = proCountingTaskService.update(proCountingTask);
//			
//	        if(listWin.size()==0&&listLose.size()==0){
//	        	result.setCode(Constant.SUCCESS);
//	        	result.setMsg("盘点无差异");
//	        }else{
//	        	result.setCode(Constant.SUCCESS);
//	        	result.setMsg("盘点差异已记录");
//	        }
//        }else{
//        	result.setCode(Constant.FAIL);
//        	result.setMsg("盘点单号不存在");
//        }
//         
//        return result;
//    }
    /** 
    * @Title: countingConfig 
    * @Description: 成品盘点确认
    * @author yanggm 
    * @date 2018年8月21日 下午3:53:11 
    * @throws 
    */
    @PostMapping("countingconfig")
    public HandResp countingConfig(@RequestBody ProCountingDto proCountingDto){
        HandResp result = new HandResp();
        String LoginName = proCountingDto.getLoginName();
        //根据盘点单号查询
        ProCountingTask countingByCode = new ProCountingTask();
        countingByCode.setCountingCode(proCountingDto.getCountingCode());
        List<ProCountingTask> tmp = proCountingTaskService.queryByAny(countingByCode);
        if(tmp.size()>0){
        	proCountingDto.setModel(tmp.get(0).getModel());
	        //根据盘点单号，查询盘亏数据
	        List<ProStorageSn> listLose = proStorageSnService.queryLose(proCountingDto);
	        if(listLose.size()>0){
	        	//盘亏，存在盘亏数据
	        	for(ProStorageSn proStorageSn:listLose){
	        		//创建盘亏差异记录
	        		ProCountingDifference proCountingDifference = new ProCountingDifference();
	        		proCountingDifference.setCountingCode(proCountingDto.getCountingCode());
	        		proCountingDifference.setCountingTime(new Date());//盘点时间记录当前时间了
	        		proCountingDifference.setProBarcode(proStorageSn.getBarcode());
	        		proCountingDifference.setModel(proStorageSn.getModel());
	        		proCountingDifference.setDifferType("1");//盘亏
	        		proCountingDifference.setUserDefined1(Reason(proStorageSn.getBarcode()));
	        		proCountingDifference.setDifferStatus("0");//未处理
	        		proCountingDifference.setWlCode(proStorageSn.getWlCode());
	        		proCountingDifference.setKeepBy(LoginName);//保管员记录为当前登录人了
	        		proCountingDifference.setGmtCreate(new Date());
	        		proCountingDifference.setActiveFlag("1");
	        		proCountingDifference.setCreateBy(LoginName);
	        		
	        		
	        		Resp resp = proCountingDifferenceService.create(proCountingDifference);
	        		result.setCode(resp.getCode());
	        		if(resp.getMsg() != null && !resp.getMsg().equals("")){
	        			result.setMsg(result.getMsg());
	        		}
	        	}
	        }
//	        //根据盘点单号，查询盘盈数据
//	        List<ProCountingRecords> listWin = proCountingRecordsService.queryWin(proCountingDto);
//	        if(listWin.size()>0){
//	        	for(ProCountingRecords proCountingRecords : listWin){
//	        		//创建盘盈差异记录
//	        		ProCountingDifference proCountingDifference = new ProCountingDifference();
//	        		proCountingDifference.setCountingCode(proCountingDto.getCountingCode());
//	        		//proCountingDifference.setPurchaseNo(rawCountingRecords.getPurchaseNo());
//	        		proCountingDifference.setCountingTime(new Date());//盘点时间记录当前时间了
//	        		proCountingDifference.setProBarcode(proCountingRecords.getBarcode());
//	        		proCountingDifference.setModel(proCountingRecords.getModel());
//	        		proCountingDifference.setDifferType("0");//盘盈
//	        		proCountingDifference.setUserDefined1(Reason(proCountingRecords.getBarcode()));
//	        		proCountingDifference.setDifferStatus("0");//未处理
//	        		proCountingDifference.setWlCode(proCountingRecords.getWlCode());
//	        		proCountingDifference.setKeepBy(LoginName);//保管员记录为当前登录人了
//	        		proCountingDifference.setGmtCreate(new Date());
//	        		proCountingDifference.setActiveFlag("1");
//	        		proCountingDifference.setCreateBy(LoginName);
//	        		Resp resp = proCountingDifferenceService.create(proCountingDifference);	
//	        		result.setCode(resp.getCode());
//	        		result.setMsg(result.getMsg());
//	        	}
//	        }
	        ProCountingTask proCountingTask = new ProCountingTask();
	        proCountingTask.setLastModifiedBy(proCountingDto.getLoginName());
	        proCountingTask.setGmtModified(new Date());
			//根据countingTaskId修改
	        proCountingTask.setCountingTaskId(tmp.get(0).getCountingTaskId());

			//将盘点任务状态置为盘点结束
			proCountingTask.setStauts("2");
			Resp respStauts = proCountingTaskService.update(proCountingTask);
			ProCountingDifference difference = new ProCountingDifference();
			difference.setCountingCode(proCountingDto.getCountingCode());
			String flag = Constant.SUCCESS;
			if(flag.equals(respStauts.getCode())){
//				if(listWin.size()==0&&listLose.size()==0){
				if(0==proCountingDifferenceService.queryCount(difference)){
		        	result.setCode(Constant.SUCCESS);
		        	result.setMsg("盘点无差异");
		        }else if(flag.equals(result.getCode())){
		        	result.setMsg("盘点差异已记录");
		        }
			}else {
				result.setCode(respStauts.getCode());
	        	result.setMsg(respStauts.getMsg());
			}
        }else{
        	result.setCode(Constant.FAIL);
        	result.setMsg("盘点单号不存在");
        }
         
        return result;
    }

    public String Reason(String barcode){
    	ProHandOutSn handOutSn = new ProHandOutSn();
    	handOutSn.setBarcode(barcode);
    	Long handOut = proHandOutSnService.queryCount(handOutSn);
    	ProSellOutSn sellOutSn = new ProSellOutSn();
    	sellOutSn.setBarcode(barcode);
    	Long sellOut = proSellOutSnService.queryCount(sellOutSn);
//    	ProScrapOutSn scrapOutSn = new ProScrapOutSn();
//    	scrapOutSn.setBarcode(barcode);
//    	Long scrapOut = proScrapOutSnService.queryCount(scrapOutSn);
    	
    	ProOrderInSn orderInSn = new ProOrderInSn();
    	orderInSn.setBarcode(barcode);
    	Long in = proOrderInSnService.queryCount(orderInSn);
    	
    	if(in>0){
    		if(sellOut>0){return "销售出库";}
    		if(handOut>0){return "手工出库";}
//    		if(scrapOut>0){return "报废出库";}
    		return "丢失或漏扫";
    	}
    	if(sellOut<=0 && handOut<=0 && in<=0){
    		return "未入库";
    	}
    	return "未入库却出库了";
    }
}
